package preformance;

import io.appium.java_client.android.Activity;
import io.appium.java_client.android.AndroidDriver;
import org.junit.Test;
import org.junit.jupiter.api.*;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;

import java.net.MalformedURLException;
import java.net.URL;

@DisplayName("开屏速度")
public class SplashAdv {
    private static AndroidDriver driver;

    @BeforeEach
    public void setUp() throws MalformedURLException {
        System.out.println("aaaaaaaaaaa");
    }

    @Test
    public void sampleTest1() throws InterruptedException, MalformedURLException {
        DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
        desiredCapabilities.setCapability("platformName", "Android");
        desiredCapabilities.setCapability("platformVersion", "7");
        desiredCapabilities.setCapability("deviceName", "xx");
//        desiredCapabilities.setCapability("appPackage", "com.ygkj.chelaile.standard");
//        desiredCapabilities.setCapability("appActivity", "dev.xesam.chelaile.app.module.func.SplashActivity");
        desiredCapabilities.setCapability("noReset", true);
        desiredCapabilities.setCapability("desiredCapabilities", true);//可以输入中文
        //  desiredCapabilities.setCapability("app","D:\\my-mayun\\apk\\chelaile_app_chelaile_www_lastest.apk");
        //  desiredCapabilities.setCapability("autoGrantPermissions",true);//自动同意一些授权
        URL remoteUrl = new URL("http://127.0.0.1:4723/wd/hub");

        driver = new AndroidDriver(remoteUrl, desiredCapabilities);
        for (int i = 0; i < 10; i++) {
            long b = System.currentTimeMillis();
            Activity a = new Activity("com.ygkj.chelaile.standard", "dev.xesam.chelaile.app.module.func.SplashActivity");
            driver.startActivity(a);
            try {
                //存在写不靠谱的情况 enable=true ，但是clickable是false
//                WebDriverWait explicit = new WebDriverWait(driver, 100);
//                WebElement e = explicit.until(ExpectedConditions.elementToBeClickable(
//                        By.id("com.ygkj.chelaile.standard:id/cll_splash_skip")));
                //    //通过uiautomator定位clickable属性为true的元素并点击
                while (true) {
                    try {
                        WebElement d = driver.findElementByAndroidUIAutomator("new UiSelector().resourceId(\"com.ygkj.chelaile.standard:id/cll_splash_skip\").clickable(true)");
                        if (d != null) {
                            //https://developer.android.com/reference/android/support/test/uiautomator/UiSelector#resourceid
                            d.click();
                            break;
                        }

                        System.out.println("点击了");
                    } catch (Exception e1) {
                        // e1.printStackTrace();
                    }
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
            Thread.sleep(5000);
            System.out.println(driver.getSessionId() + "元素时间 耗时ms=" + (System.currentTimeMillis() - b));
            driver.closeApp();
        }


    }
}
